<%- include('../layout/header.ejs')%>
    <div class="container">
        <h1>添加文章</h1>
        <hr>
        <form id="form">
            <input type="hidden" name="userid" value="<%=user.id%>">
            <div class="form-group">
                <label>文章标题</label>
                <input type="text" class="form-control" name="title">
            </div>
            <div class="form-group">
                <label>文章内容：</label>
                <textarea name="content" class="form-control" id="editor"></textarea>
            </div>
            <input type="submit" class="btn btn-success" value="发布文章">
        </form>
    </div>
    <script>
        $(function() {
            var mditor = Mditor.fromTextarea(document.getElementById('editor'));
            // 监听提交
            $('#form').on('submit', (e) => {
                e.preventDefault()
                $.ajax({
                    type: 'post',
                    url: '/sendArticle',
                    data: $('#form').serialize(),
                    success: res => {
                        if (res.status != 200) return alert(res.msg);
                        location.href = '/article/info/' + res.insertId;
                    }
                })
            })
        })
    </script>
    <%- include('../layout/footer.ejs')%>